package com.translate.web.service.impl;

import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.PreStoredRewardsConfMapper;
import com.translate.entity.PreStoredRewardsConf;
import com.translate.vo.contract.IPreStoredRewardsConfVO;
import com.translate.web.service.IPreStoredRewardsConfService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class PreStoredRewardsConfServiceImpl extends BaseTranslateServiceImpl<PreStoredRewardsConf>
		implements IPreStoredRewardsConfService {
	
	private static final Logger logger = LoggerFactory.getLogger(PreStoredRewardsConfServiceImpl.class);
	
	public PreStoredRewardsConfServiceImpl(PreStoredRewardsConfMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(PreStoredRewardsConf data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(PreStoredRewardsConf data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<PreStoredRewardsConf> getEntityClass() {
		return PreStoredRewardsConf.class;
	}

	@Override
	public <VO extends IPreStoredRewardsConfVO> List<VO> getListFromCache(Class<VO> target) {
		return this.entityConvertToVo(this.listToCache(RedisConstant.PRE_STORE_KEY, 100), target);
	}
	
	@Resource
	public void setRedisTemplate(RedisTemplate<String, ? extends Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	public PreStoredRewardsConf getPreStoredRewardsConf(BigDecimal money) {
		
		List<PreStoredRewardsConf> listToCache = this.listToCache(RedisConstant.PRE_STORE_KEY, 100);
		
		BigDecimal priceEnd;
		BigDecimal priceStart;
		
		PreStoredRewardsConf recive = null;
		
		// 以上
		
		List<PreStoredRewardsConf> moreThan = new ArrayList<PreStoredRewardsConf>();
		
		
		for (PreStoredRewardsConf preStoredRewardsConf : listToCache) {
			
			if (preStoredRewardsConf.getType().byteValue() == 0) {
				moreThan.add(preStoredRewardsConf);
				continue;
			}
			
			priceStart = preStoredRewardsConf.getPriceStart();
			
			priceEnd = preStoredRewardsConf.getPriceEnd();
			
			logger.debug("priceStart: {}, money: {}, priceEnd: {}", priceStart, money, priceEnd);
			
			// -1(小于)、0(等于)或1(大于)，因为此BigDecimal在数字上小于、等于或大于值。
			if (0 >= priceStart.compareTo(money) &&
				0 <= priceEnd.compareTo(money)
			) {
				return preStoredRewardsConf;
			}
			
		}
		
		
		moreThan.sort((f,s) -> f.getPriceStart().compareTo(s.getPriceStart()));
		
		for (PreStoredRewardsConf preStoredRewardsConf : moreThan) {
			
		
//			if (1 == priceEnd.compareTo(money)) {
//				return preStoredRewardsConf;
//			}
			
			/** 	50000 < 60000
			 *   s < x < e
			 *   
			 *   s > x > e
			 */
			
			priceStart = preStoredRewardsConf.getPriceStart();
			
			logger.debug("priceStart: {}, money: {}", priceStart, money);
			
			if(0 >= priceStart.compareTo(money)) {
				recive = preStoredRewardsConf;
			}
			
//			return preStoredRewardsConf;
		}
		return recive;
		
	}

	@Override
	public String getReward(BigDecimal money, int rewardType) {
		
		PreStoredRewardsConf preStoredRewardsConf = this.getPreStoredRewardsConf(money);
		
		String reward = "";
		
		if (null == preStoredRewardsConf) {
			return reward;
		}
		
		if (0 == rewardType) {
			reward = preStoredRewardsConf.getDiscount().toString();
		} else if (1 == rewardType) {
			reward = preStoredRewardsConf.getWelfare();
		}
		
		return reward;
	}
	
}